const APP_SECRET = "QIAODAIMACECRETKEY";
import e from "cors";
import jwt from "jsonwebtoken";

// function getUserId(token: any | null | undefined) {
//   if (!token) {
//     throw new Error("token is null");
//   }

//   const userId = token.userId;
//   if (!userId) {
//     throw new Error("user is is null");
//   }

//   return userId;
// }

function getUserId(req: any | null | undefined) {
  if (!req) {
    throw new Error("request is error");
  }
  const token = req.headers.authorization;
  console.log("token:", typeof token);
  if (token != "null") {
    const { userId } = jwt.verify(token.split(" ")[1], APP_SECRET);
    return userId;
  } else {
    return null;
    // throw new Error("Not authenticated");
  }
}

export { APP_SECRET, getUserId };
